
# -*- encoding=utf-8 -*-
# 1、使用2号接口的语音识别模块，注意需要固件版本V1.2.3版本

from froModuleDrivers.generalDeviceDriver import GeneralDeviceDriver
from froModuleDrivers.nioManager import NioManager
# import time
# from froModuleDrivers import LOGGER


# LOGGER.set_lvl("debug")

driver = GeneralDeviceDriver("192.168.50.218", 4001)
manager = NioManager(driver)
manager.run()

driver.showVersion()
driver.setWaitTime(0.4) # 设置指令返回的等待时间为0.4秒

# 1、自动清除的方式
while True:
    speech_value = driver.recognizeSpeechAndClearCache(2) # 获取语音识别结果，然后清除缓存的数据
    if speech_value == -1:
        print(f"查询失败")
    elif speech_value == 0:
        print(f"未识别到语音")
    else:
        print(f"语音识别的编号是：{speech_value},内容是：{driver.voiceEncodeV3[speech_value]}")
        # 接下来可以根据识别结果做处理
        # Todo

# 2、手动清除
# while True:
#     speech_value = driver.recognizeSpeech(2) # 获取语音识别结果，然后清除缓存的数据
#     if speech_value == -1:
#         print(f"查询失败")
#     elif speech_value == 0:
#         print(f"未识别到语音")
#     else:
#         print(f"语音识别的编号是：{speech_value},内容是：{driver.voiceEncodeV3[speech_value]}")
#         print(f"清除缓存的编号")
#         driver.clearCachedSpeech(2) # 清除缓存数据
#         # 接下来可以根据识别结果做处理
#         # Todo


manager.shutdownGracefully() # 等待指令执行完毕后系统退出
